home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / term / extras / source / term-source.lha / termLists.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  6KB  |  305 lines

  1. /*
  2. **    termLists.c
  3. **
  4. **    Generic list management routines
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* ClearGenericList(struct GenericList *List):
  13.      *
  14.      *    Clear a generic list, free its contents.
  15.      */
  16.  
  17. VOID __regargs
  18. ClearGenericList(struct GenericList *List)
  19. {
  20.     ObtainSemaphore(&List -> ListSemaphore);
  21.  
  22.     FreeList((struct List *)&List -> ListHeader);
  23.  
  24.     List -> ListCount = 0;
  25.  
  26.     ReleaseSemaphore(&List -> ListSemaphore);
  27. }
  28.  
  29.     /* DeleteGenericList(struct GenericList *List):
  30.      *
  31.      *    Free a generic list, including its contents.
  32.      */
  33.  
  34. VOID __regargs
  35. DeleteGenericList(struct GenericList *List)
  36. {
  37.     ClearGenericList(List);
  38.  
  39.     FreeVecPooled(List);
  40. }
  41.  
  42.     /* CreateGenericList():
  43.      *
  44.      *    Create a generic list.
  45.      */
  46.  
  47. struct GenericList *
  48. CreateGenericList()
  49. {
  50.     struct GenericList *List;
  51.  
  52.     if(List = (struct GenericList *)AllocVecPooled(sizeof(struct GenericList),MEMF_ANY | MEMF_CLEAR | MEMF_PUBLIC))
  53.     {
  54.         NewList((struct List *)&List -> ListHeader);
  55.  
  56.         InitSemaphore(&List -> ListSemaphore);
  57.  
  58.         List -> ListCount = 0;
  59.     }
  60.  
  61.     return(List);
  62. }
  63.  
  64.     /* FirstGenericListNode(struct GenericList *List):
  65.      *
  66.      *    Pick the first node in a generic list.
  67.      */
  68.  
  69. struct Node * __regargs
  70. FirstGenericListNode(struct GenericList *List)
  71. {
  72.     struct Node *Node;
  73.  
  74.     ObtainSemaphore(&List -> ListSemaphore);
  75.  
  76.     if(List -> ListHeader . mlh_Head -> mln_Succ)
  77.         Node = List -> ListNode = (struct Node *)List -> ListHeader . mlh_Head;
  78.     else
  79.         Node = NULL;
  80.  
  81.     ReleaseSemaphore(&List -> ListSemaphore);
  82.  
  83.     return(Node);
  84. }
  85.  
  86.     /* LastGenericListNode(struct GenericList *List):
  87.      *
  88.      *    Pick the last node in a generic list.
  89.      */
  90.  
  91. struct Node * __regargs
  92. LastGenericListNode(struct GenericList *List)
  93. {
  94.     struct Node *Node;
  95.  
  96.     ObtainSemaphore(&List -> ListSemaphore);
  97.  
  98.     if(List -> ListHeader . mlh_Head -> mln_Succ)
  99.         Node = List -> ListNode = (struct Node *)List -> ListHeader . mlh_TailPred;
  100.     else
  101.         Node = NULL;
  102.  
  103.     ReleaseSemaphore(&List -> ListSemaphore);
  104.  
  105.     return(Node);
  106. }
  107.  
  108.     /* NextGenericListNode(struct GenericList *List):
  109.      *
  110.      *    Pick the next successive node in a generic list.
  111.      */
  112.  
  113. struct Node * __regargs
  114. NextGenericListNode(struct GenericList *List)
  115. {
  116.     struct Node *Node;
  117.  
  118.     ObtainSemaphore(&List -> ListSemaphore);
  119.  
  120.     if(List -> ListNode)
  121.     {
  122.         if(List -> ListNode -> ln_Succ -> ln_Succ)
  123.             Node = List -> ListNode = List -> ListNode -> ln_Succ;
  124.         else
  125.             Node = NULL;
  126.  
  127.         ReleaseSemaphore(&List -> ListSemaphore);
  128.     }
  129.     else
  130.     {
  131.         ReleaseSemaphore(&List -> ListSemaphore);
  132.  
  133.         Node = FirstGenericListNode(List);
  134.     }
  135.  
  136.     return(Node);
  137. }
  138.  
  139.     /* PrevGenericListNode(struct GenericList *List):
  140.      *
  141.      *    Pick the preceding node in a generic list.
  142.      */
  143.  
  144. struct Node * __regargs
  145. PrevGenericListNode(struct GenericList *List)
  146. {
  147.     struct Node *Node;
  148.  
  149.     ObtainSemaphore(&List -> ListSemaphore);
  150.  
  151.     if(List -> ListNode)
  152.     {
  153.         if(List -> ListNode -> ln_Pred -> ln_Pred)
  154.             Node = List -> ListNode = List -> ListNode -> ln_Pred;
  155.         else
  156.             Node = NULL;
  157.  
  158.         ReleaseSemaphore(&List -> ListSemaphore);
  159.     }
  160.     else
  161.     {
  162.         ReleaseSemaphore(&List -> ListSemaphore);
  163.  
  164.         Node = LastGenericListNode(List);
  165.     }
  166.  
  167.     return(Node);
  168. }
  169.  
  170.     /* DeleteGenericListNode(struct GenericList *List,struct Node *Node):
  171.      *
  172.      *    Delete a single node from a generic list.
  173.      */
  174.  
  175. VOID __regargs
  176. DeleteGenericListNode(struct GenericList *List,struct Node *Node)
  177. {
  178.     ObtainSemaphore(&List -> ListSemaphore);
  179.  
  180.     if(!Node)
  181.         Node = List -> ListNode;
  182.  
  183.     if(Node)
  184.     {
  185.         if(Node == List -> ListNode)
  186.         {
  187.             if(Node -> ln_Succ -> ln_Succ)
  188.                 List -> ListNode = Node -> ln_Succ;
  189.             else
  190.             {
  191.                 if(Node -> ln_Pred -> ln_Pred)
  192.                     List -> ListNode = Node -> ln_Pred;
  193.                 else
  194.                     List -> ListNode = NULL;
  195.             }
  196.         }
  197.  
  198.         FreeNode(Node);
  199.  
  200.         List -> ListCount--;
  201.     }
  202.  
  203.     ReleaseSemaphore(&List -> ListSemaphore);
  204. }
  205.  
  206.     /* CreateGenericListNode(LONG Size,STRPTR Name):
  207.      *
  208.      *    Create a new generic list node.
  209.      */
  210.  
  211. struct Node * __regargs
  212. CreateGenericListNode(LONG Size,STRPTR Name)
  213. {
  214.     struct Node    *Node;
  215.     LONG         Head;
  216.  
  217.     if(Size < sizeof(struct Node))
  218.         Head = Size = sizeof(struct Node);
  219.     else
  220.         Head = Size;
  221.  
  222.     if(Name)
  223.         Size += strlen(Name) + 1;
  224.  
  225.     if(Node = (struct Node *)AllocVecPooled(Size,MEMF_ANY | MEMF_CLEAR | MEMF_PUBLIC))
  226.     {
  227.         if(Name)
  228.         {
  229.             Node -> ln_Name = ((char *)Node) + Head;
  230.  
  231.             strcpy(Node -> ln_Name,Name);
  232.         }
  233.         else
  234.             Node -> ln_Name = NULL;
  235.     }
  236.  
  237.     return(Node);
  238. }
  239.  
  240.     /* AddGenericListNode(struct GenericList *List,struct Node *Node,BYTE Mode):
  241.      *
  242.      *    Add a node to a generic list.
  243.      */
  244.  
  245. VOID __regargs
  246. AddGenericListNode(struct GenericList *List,struct Node *Node,BYTE Mode)
  247. {
  248.     ObtainSemaphore(&List -> ListSemaphore);
  249.  
  250.     switch(Mode)
  251.     {
  252.         case ADD_GLIST_BOTTOM:
  253.  
  254.             AddTail((struct List *)&List -> ListHeader,Node);
  255.             break;
  256.  
  257.         case ADD_GLIST_TOP:
  258.  
  259.             AddHead((struct List *)&List -> ListHeader,Node);
  260.             break;
  261.  
  262.         case ADD_GLIST_BEHIND:
  263.  
  264.             if(List -> ListNode)
  265.                 Insert((struct List *)&List -> ListHeader,Node,List -> ListNode);
  266.             else
  267.                 AddTail((struct List *)&List -> ListHeader,Node);
  268.  
  269.             break;
  270.  
  271.         case ADD_GLIST_BEFORE:
  272.  
  273.             if(List -> ListNode && List -> ListNode != (struct Node *)List -> ListHeader . mlh_Head)
  274.                 Insert((struct List *)&List -> ListHeader,Node,List -> ListNode -> ln_Pred);
  275.             else
  276.                 AddHead((struct List *)&List -> ListHeader,Node);
  277.  
  278.             break;
  279.     }
  280.  
  281.     List -> ListNode = Node;
  282.     List -> ListCount++;
  283.  
  284.     ReleaseSemaphore(&List -> ListSemaphore);
  285. }
  286.  
  287.     /* GenericListCount(struct GenericList *List):
  288.      *
  289.      *    Return the number of generic list entries.
  290.      */
  291.  
  292. LONG __regargs
  293. GenericListCount(struct GenericList *List)
  294. {
  295.     LONG Count;
  296.  
  297.     ObtainSemaphore(&List -> ListSemaphore);
  298.  
  299.     Count = List -> ListCount;
  300.  
  301.     ReleaseSemaphore(&List -> ListSemaphore);
  302.  
  303.     return(Count);
  304. }
  305.